package net.martinimix.edesk.search;

import com.bluemartini.dna.BMContext;
import com.bluemartini.dna.BMException;
import com.bluemartini.dna.BusinessObjectArray;
import com.bluemartini.dna.DNAList;
import com.bluemartini.dna.DNAStringArray;

/**
 * Provides a single search term based record search.
 * 
 * @author Scott Rossillo
 *
 */
public final class SingleTermRecordSearch extends AbstractRecordSearch implements RecordSearch {
	
	/**
	 * Creates a new single term record search.
	 */
	public SingleTermRecordSearch() {
		super();
	}

	/* (non-Javadoc)
	 * @see net.martinimix.edesk.search.AbstractRecordSearch#searchInternal(java.lang.String, int, int, com.bluemartini.dna.DNAStringArray)
	 */
	protected DNAList searchInternal(
			final String searchTerms, 
			final int firstRow, 
			final int maxRows,
			final DNAStringArray objectTypes) throws BMException {

		final DNAList response = new DNAList();
		final BusinessObjectArray searchResults = new BusinessObjectArray();
		
		if(objectTypes.contains("PRODUCT")) {
			searchResults.addElements(createProductSearchResults(searchTerms));
		}
		
		if(objectTypes.contains("SKU")) {
			searchResults.addElements(createSkuSearchResults(searchTerms));
		}
		
		response.setInteger("totalRows", searchResults.size());
		 
		// Note this is a highly inefficient paging method.  Offset and max could be passed to Endeca
		// but that's complicated due to the fact that multiple object types may be requested.
		if(searchResults.size() > maxRows) {
			
			final BusinessObjectArray filteredResults = new BusinessObjectArray(maxRows);
			
			for(int i = firstRow; i < (firstRow + maxRows) && i < searchResults.size(); i++) {
				filteredResults.addElement(searchResults.elementAt(i));
			}
			
			response.setBusinessObjectArrayRef("OBJECT_ARRAY", filteredResults);
			
		} else {
			response.setBusinessObjectArrayRef("OBJECT_ARRAY", searchResults);
		}
		
		return response;
	}

	private BusinessObjectArray createProductSearchResults(final String searchTerms) throws BMException {
		
		final DNAList config = BMContext.getConfig("pdc.endeca.search.dna");
		final BusinessObjectArray searchResults = new BusinessObjectArray();
		final BusinessObjectArray productResults;
		
		final ProductSearch productSearch =
			new ProductSearch(
					config.getString("endecaHost"),
					config.getInteger("endecaPort", 5555), // default ENE port
					config.getString("productIdProperty"));
		
		productResults = productSearch.findProducts(searchTerms);
		
		for(int i = 0; i < productResults.size(); i++) {
			searchResults.addElementRef(getGenericObject(productResults.elementAt(i)));
		}
		
		return searchResults;
	}
	
	private BusinessObjectArray createSkuSearchResults(final String searchTerms) throws BMException {
		
		final DNAList config = BMContext.getConfig("pdc.endeca.search.dna");
		final BusinessObjectArray searchResults = new BusinessObjectArray();
		final BusinessObjectArray skuResults;
		
		final SkuSearch skuSearch =
			new SkuSearch(
					config.getString("endecaHost"),
					config.getInteger("endecaPort", 5555), // default ENE port
					config.getString("skuIdProperty"));
		
		skuResults = skuSearch.findSkus(searchTerms);
		
		for(int i = 0; i < skuResults.size(); i++) {
			searchResults.addElementRef(getGenericObject(skuResults.elementAt(i)));
		}
		
		return searchResults;
	}
}
